<template>
  <div>
    <el-form :inline="true" :model="formInline" class="demo-form-inline">
      <el-form-item>
        <el-input
          v-model="formInline.key"
          placeholder="请输入对象key"
          size="mini"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-input
          v-model="formInline.name"
          placeholder="请输入对象名称"
          size="mini"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          @click="onSubmit"
          size="mini"
          icon="el-icon-search"
          >查找</el-button
        >
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          @click="addopen"
          size="mini"
          icon="el-icon-edit"
          >添加</el-button
        >
        <el-button type="primary" size="mini" icon="el-icon-download" @click="exportTable"
          >导出</el-button
        >
      </el-form-item>
    </el-form>
    <div class="tables">
      <template>
        <el-table border :data="tableData" style="width: 100%">
          <el-table-column prop="key" label="对象key" align="center">
          </el-table-column>
          <el-table-column prop="name" label="对象名称" align="center">
          </el-table-column>
          <el-table-column prop="type" label="对象类型" align="center">
          </el-table-column>
          <el-table-column prop="size" label="对象大小" align="center">
          </el-table-column>
          <el-table-column prop="url" label="图片" align="center">
            <template slot-scope="scope">
              <img :src="scope.row.url" width="50" height="50" alt="" />
            </template>
          </el-table-column>
          <el-table-column prop="url" label="图片链接" align="center">
          </el-table-column>
          <el-table-column prop="address" label="操作" align="center">
            <template slot-scope="scope">
              <el-button
                type="primary"
                size="mini"
                @click="handleEdit(scope.$index, scope.row)"
              >
                编辑
              </el-button>
              <el-button
                size="mini"
                type="danger"
                @click="dels(scope.$index, scope.row)"
              >
                删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </template>
      <div class="page">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="get.page"
          :page-sizes="[10, 20, 30, 40, 50, 100]"
          :page-size="get.limit"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </div>
    </div>
    <!-- ========================= -->
    <el-dialog title="编辑" :visible.sync="dialogVisible" width="50%">
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item v-if="flag" label="对象名称">
          <el-input
            v-model="form.name"
            style="width: 300px"
            size="mini"
          ></el-input>
        </el-form-item>
        <el-upload
          v-if="!flag"
          class="upload-demo"
          auto-upload
          action="/api/admin/storage/create"
          :on-change="handleChange"
        >
          <el-button size="small" type="primary">点击上传</el-button>
        </el-upload>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button v-if="flag" type="primary" @click="update">确 定</el-button>
        <el-button v-if="!flag" type="primary" @click="addlist"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { downloadXlsx } from "../../units/xlx.js";
import { getList, redact, del, add, search } from "@/api/systemm/objects.js";
export default {
  data() {
    return {
      flag: true,
      formLabelWidth: "120px",
      dialogVisible: false,
      form: {},
      tableData: [],
      formInline: {},
      total: 0,
      get: {
        page: 1,
        limit: 20,
        name: "",
        sort: "add_time",
        order: "desc",
      },
    };
  },
  created() {
    this.getdata();
  },
  methods: {
    handleChange(file) {
      console.log(file);
      this.getdata();
      this.dialogVisible = false;
    },
    getdata() {
      getList(this.get).then((res) => {
        console.log(res);
        if ((res.data.errmsg = "成功")) {
          this.tableData = res.data.list;
          this.total = res.data.total;
          console.log(this.tableData);
        }
      });
    },
    // 查询
    onSubmit() {
      this.get.page = 1;
      this.get.name = this.formInline.name;
      this.get.key = this.formInline.key;
      this.getdata();
    },

    // 编辑
    handleEdit(a, b) {
      this.flag = true;
      this.dialogVisible = true;
      this.form = b;
    },
    //编辑确定
    update() {
      redact(this.form).then((res) => {
        if (res.errmsg == "成功") {
          // console.log(res);
          this.getdata();
          this.form = {};
          this.dialogVisible = false;
          this.$notify({
            title: "成功",
            message: "更新管理员成功",
            type: "success",
          });
        }
      });
    },
    // 删除
    dels(a, b) {
      del(b).then((res) => {
        if (res.errmsg == "成功") {
          this.getdata();
          this.$notify({
            title: "成功",
            message: "删除成功",
            type: "success",
          });
        }
      });
    },
    // 添加打开
    addopen() {
      this.form = {};
      this.flag = false;
      this.dialogVisible = true;
    },
    //添加
    addlist() {
      add(this.form).then((res) => {
        console.log(res);
        if (res.errmsg == "成功") {
          this.getdata();
          this.form = {};
          this.dialogVisible = false;
          this.$notify({
            title: "成功",
            message: "添加成功",
            type: "success",
          });
        } else if (res.errmsg == "角色已经存在") {
          this.$notify({
            title: "失败",
            message: res.errmsg,
            type: "error",
          });
        }
      });
    },
    //导出
    exportTable() {
      let headers = ['对象key', '对象名称', '对象类型', '对象大小', '图片', '图片链接'];
      let list = [headers];
      for (let i = 0; i < this.tableData.length; i++) {
        let arr = [
          this.tableData[i].key,
          this.tableData[i].name,
          this.tableData[i].type,
          this.tableData[i].size,
          this.tableData[i].url,
          this.tableData[i].url,
        ];
        list.push(arr);
      }
      downloadXlsx(list, "存储对象.xlsx");
    },
    //分页
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.get.limit = val;
      this.getdata();
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.get.page = val;
      this.getdata();
    },
  },
};
</script>

<style lang="scss" scoped>
::v-deep .el-table td,
::v-deep .el-table th {
  padding: 4px 0px;
}
.el-table {
  font-size: 12px;
}
.page {
  margin-top: 40px;
  display: flex;
  justify-content: left;
}
</style>